package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallComb;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallText;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallUserData;
import com.yanqu.road.entity.activity.xiaowuRecall.config.XiaowuConfig;
import com.yanqu.road.pb.activity.XiaowuRecallProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.Map;

import static com.yanqu.road.server.gameplayer.module.activity.xiaowuRecall.XiaowuRecallModule.ARRAYNUM_ADD_TO_CELLID;

public class XiaowuRecallPb {
    public static XiaowuRecallProto.SyncXiaowuConfigRespMsg.Builder parseXiaowuConfigRespMsg(XiaowuConfig xiaowuConfig,String language) {
        XiaowuRecallProto.SyncXiaowuConfigRespMsg.Builder builder = XiaowuRecallProto.SyncXiaowuConfigRespMsg.newBuilder();
        for (XiaowuRecallComb xiaowuRecallComb : xiaowuConfig.getXiaowuRecallCombList()) {
            XiaowuRecallProto.CombDetail.Builder detailBuilder = XiaowuRecallProto.CombDetail.newBuilder();
            detailBuilder.setCombId(xiaowuRecallComb.getCellId());
            for (int i = 0; i < xiaowuRecallComb.getComb().size(); i++) {
                detailBuilder.addCellsId(xiaowuRecallComb.getComb().get(i));
            }
            detailBuilder.setReward(xiaowuRecallComb.getRewards());
            detailBuilder.setType(xiaowuRecallComb.getType());

            builder.addCombDetail(detailBuilder);
        }
        for (XiaowuRecallText xiaowuRecallText:xiaowuConfig.getXiaowuRecallTextList()){
            XiaowuRecallProto.XiaowuPageInfo.Builder textBuilder = XiaowuRecallProto.XiaowuPageInfo.newBuilder();
            textBuilder.setPageId(xiaowuRecallText.getId());
            textBuilder.setPageNum(xiaowuRecallText.getPageNum());
            textBuilder.setPageText(ServerLanguageMgr.getContent(xiaowuRecallText.getValue(),language));

            builder.addPageInfo(textBuilder);
        }
        return builder;
    }

    public static XiaowuRecallProto.XiaowuRecallUserDataMsg.Builder createDataMsgBuilder(XiaowuRecallUserData userData) {
        XiaowuRecallProto.XiaowuRecallUserDataMsg.Builder dataBuilder = XiaowuRecallProto.XiaowuRecallUserDataMsg.newBuilder();

        //下发基本信息
        dataBuilder.setPage(userData.getPage());            //页面数
        dataBuilder.setScore(userData.getScore());          //积分
        dataBuilder.setSoul(userData.getSoul());            //魂

        //遍历数组生成格子解锁状态
        for (int i = 0; i < 15; i++) {
            XiaowuRecallProto.SliceCondition.Builder sliceBuilder = XiaowuRecallProto.SliceCondition.newBuilder();
            if (userData.getSliceCondition()[i] == 0) {
                sliceBuilder.setCellId(i + ARRAYNUM_ADD_TO_CELLID);
                sliceBuilder.setUnlocked(false);
                dataBuilder.addSliceList(sliceBuilder);
            } else {
                sliceBuilder.setCellId(i + ARRAYNUM_ADD_TO_CELLID);
                sliceBuilder.setUnlocked(true);
                dataBuilder.addSliceList(sliceBuilder);
            }
        }
        //遍历map加入奖励获取情况
        Map<Integer, Integer> rewardMap = userData.getAwarded();
        for (Integer key : rewardMap.keySet()) {
            XiaowuRecallProto.CombReward.Builder combBuilder = XiaowuRecallProto.CombReward.newBuilder();
            combBuilder.setCombId(key);
            if (rewardMap.get(key) == 0) {
                combBuilder.setHaveGetReward(false);
            } else {
                combBuilder.setHaveGetReward(true);
            }
            dataBuilder.addRewardList(combBuilder);
        }
        return dataBuilder;
    }
}
